﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Toenda.Foundation.Windows.Forms {
	/// <summary>
	/// Class DropDownButton
	/// </summary>
	public partial class DropDownButton : UserControl {
		public delegate void DropDownButtonClickEventHandler(DropDownButtonClickEventArgs e);
		public event DropDownButtonClickEventHandler DropDownButtonClick;

		private bool _mouseIsOnContextMenuStrip = false;

		// ---------------------------------------------------
		// CONSTRUCTORS
		// ---------------------------------------------------

		/// <summary>
		/// Default Ctor
		/// </summary>
		public DropDownButton() {
			InitializeComponent();

			this.timer.Interval = 1500;
		}

		// ---------------------------------------------------
		// INTERFACE IMPLEMENTATIONS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROPERTIES
		// ---------------------------------------------------

		/// <summary>
		/// Get or set the dropdownbutton text
		/// </summary>
		public string DropDownButtonText {
			get { return this.btnClick.Text; }
			set { this.btnClick.Text = value; }
		}

		/// <summary>
		/// Get the dropdownbutton menu
		/// </summary>
		public ToolStripItemCollection DropDownButtonMenu {
			get { return this.cmsDropDown.Items; }
		}

		/// <summary>
		/// Get or set the dropdownbutton target
		/// </summary>
		public string DropDownButtonTarget {
			get;
			set;
		}

		// ---------------------------------------------------
		// EVENTS
		// ---------------------------------------------------

		/// <summary>
		/// btnClick_Click
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClick_Click(object sender, EventArgs e) {
			this.OnDropDownButtonClick(
				new DropDownButtonClickEventArgs(
					this.DropDownButtonTarget
				)
			);
		}

		/// <summary>
		/// OnDropDownButtonClick
		/// </summary>
		/// <param name="e"></param>
		protected virtual void OnDropDownButtonClick(DropDownButtonClickEventArgs e) {
			if(this.DropDownButtonClick != null) {
				this.DropDownButtonClick(e);
			}
		}

		/// <summary>
		/// btnDropDown_Click
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDropDown_Click(object sender, EventArgs e) {
			Point pos = this.btnDropDown.PointToScreen(this.btnDropDown.Location);

			int x = pos.X - 115;
			int y = pos.Y + this.btnDropDown.Size.Height - 2;

			this.cmsDropDown.Show(x, y);
		}

		/// <summary>
		/// btnDropDown_MouseLeave
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDropDown_MouseLeave(object sender, EventArgs e) {
			this.timer.Enabled = true;
			this.timer.Start();
		}

		/// <summary>
		/// cmsDropDown_MouseHover
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cmsDropDown_MouseHover(object sender, EventArgs e) {
			this.timer.Stop();
			this.timer.Enabled = false;

			this._mouseIsOnContextMenuStrip = true;
		}

		/// <summary>
		/// cmsDropDown_MouseLeave
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cmsDropDown_MouseLeave(object sender, EventArgs e) {
			this._mouseIsOnContextMenuStrip = false;
		}

		/// <summary>
		/// timer_Tick
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void timer_Tick(object sender, EventArgs e) {
			if(!this._mouseIsOnContextMenuStrip) {
				this.cmsDropDown.Hide();

				this.timer.Stop();
				this.timer.Enabled = false;
			}
		}

		// ---------------------------------------------------
		// PRIVATE MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROTECTED MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PUBLIC MEMBERS
		// ---------------------------------------------------
	}

	/// <summary>
	/// DropDownButtonClickEventArgs
	/// </summary>
	public class DropDownButtonClickEventArgs : EventArgs {
		private string _target;

		/// <summary>
		/// SearchButtonClickEventArgs
		/// </summary>
		/// <param name="target"></param>
		public DropDownButtonClickEventArgs(string target) {
			this._target = target;
		}

		/// <summary>
		/// Get or set the target
		/// </summary>
		public string Target {
			get { return this._target; }
			set { this._target = value; }
		}
	}
}
